<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{
	   // 图片上传
	   public function getTadd()
	  {
	    return view('home.photo.tadd'); 
	  }


	  //文件上传
		public function upload(Request $request)
		{
			   if($request->hasFile('pname'))
			   {
            //随机文件名
            	$name = time()+rand(1,99);

             //获取后缀名
            	$ext = $request->file('pname')->getClientOriginalExtension();
        
            //检测后缀名
            	$arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr))
            {
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://la.cn/test/add";
                </script>';
            }
            	$request->file('pname')->move('./uploads/',$name.'.'.$ext); 

            	return '/uploads/'.$name.'.'.$ext; 
        	}
        }
			
	  // 查询提交数据
	  public function postInsert(Request $request)
	{
		$a = $request->file('pname')->getClientOriginalName();
	  			//检测是否有文件上传
			if($request->hasFile('pname'))
			{
				//随机文件名
				$name = md5(time()+rand(1,9999999));
				//获取后缀名
				$su = $request->file('pname')->getClientOriginalExtension();
				$arr = array('jpg','png','jpeg','tiff');
				//判断
				if(!in_array($su,$arr))
				{
					echo '<script type="text/javascript">
					alert("上传文件不合法");
					location.href="http://mao.cn/test/add";
					</script>';
				}

				//将文件移动到指定目录
				// dd($request->file('pname'));
				// $request->file('pname')->move('./uploads/');
				$request->file('pname')->move('./uploads/',$name.'.'.$su);	

			
			}
		
				$b = $request->input('pid');
					if($b == 1)
					{
						$b = '相册';
					}else{
						$b = '第二相册';
					}

			  	if(!$request->input('pid'))
			  	{
			  		return back();
			  	}

			  	$tq = $request->only('pid');
			  	// $b = $tq;
			  	$tq['path'] = '/uploads/'.$name.'.'.$su;
			  	$tq['pname'] = $a;
			  	 // $tq['pid'] = $b;
	  			$tq['ptime'] = date('Y-m-d H:i:s',time());
			  	$res = DB::table('photo')->insert($tq);

			  	if($res)
			  	{
			  		return redirect('/home/photo/listp')->with('success','图片添加成功');
			  	}else{
			  		return back()->with('error','添加失败');
			  	}
	}

		public function getListp(Request $request)
		{
			// $lb = DB::table('photo')->select('uname');
			$uid=2;
			$res=DB::table('pcate')->where('uid',$uid)->get();
			// dd($res);
		      foreach ($res as $k => $v) 
		      {
		        // dd($v->id);
		          $v->sub = DB::table('photo')->where('pid',$v->id)->get();
		          $v->num=count($v->sub);
		          $data[] = $v;
		      }
			 $dt = $v->sub;
			 // dd($data);
			 // dd($dt);
			return view('home.photo.listp',['data'=>$data],['dt'=>$dt]);
		}
}
?>

